/**
 * Created by Handsome.Ho on 2015/9/19.
 */
angular.module('schedule.controllers', []).controller('scheduleCtrl', function ($scope, $stateParams, Schedule, $state, $timeout, $ionicBackdrop, $ionicSlideBoxDelegate, TaskService) {
  $scope.weeknames = Schedule.weeknames;
  //TaskService.loadFromCloud();
  function compareDate(dt1, dt2) {
    return dt1.getYear() === dt2.getYear() && dt1.getMonth() === dt2.getMonth() && dt1.getDate() === dt2.getDate()
  }

  function getSelectedData(dt, data) {
    var now_i = 0;
    var now_j = 0;
    for (var i = 0; i < data.length; i++) {
      for (var j = 0; j < data[i].length; j++) {
        var date = data[i][j].date;
        if (data[i][j].include) {
          now_i = i;
          now_j = j;
        }
        if (compareDate(date, dt)) {
          return data[i][j];
        }
      }
    }
    return data[now_i][now_j];
  }

  function buildData(data, tasks) {
    for (var i = 0; i < data.length; i++) {
      for (var j = 0; j < data[i].length; j++) {
        data[i][j].tasks = [];
        var date = data[i][j].date;
        var num = parseInt(date.Format("yyyyMMdd"));
        for (var k = 0; k < tasks.length; k++) {
          var task = tasks[k];
          if (num >= task.sdt && num <= task.edt) {
            data[i][j].tasks.push(task);
          }
        }
      }
    }
  }

  function loadData(year, month, callback) {
    var data = Schedule.getData(year, month);
    TaskService.getTaskByMonth({
      year: year,
      month: month,
      callback: function (tasks) {
        buildData(data, tasks);
        callback(data);
      }
    });
  }

  function today() {
    var today = new Date();
    var year = today.getFullYear();
    var month = today.getMonth() + 1;
    $scope.curIndex = 1;
    loadData(year, month, function (data) {
      $scope.data = data;
      $scope.selectedData = getSelectedData(today, $scope.data);
      $scope.$apply();
    });
  }

  $scope.today = today;

  function addMonth(ym) {
    var year = ym[0];
    var month = ym[1];
    month++;
    if (month == 13) {
      year++;
      month = 1;
    }
    return [year, month];
  }

  function minuMonth(ym) {
    var year = ym[0];
    var month = ym[1];
    month--;
    if (month == 0) {
      year--;
      month = 12;
    }
    return [year, month];
  }

  $scope.$watch('selectedData', function () {
    if ($scope.selectedData) {
      var date = $scope.selectedData.date;
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      $scope.currentYM = year + '-' + month;
    }
  })

  $scope.$watch('currentYM', function () {
    if ($scope.currentYM) {
      var ym = $scope.currentYM.split('-');
      var ym0 = minuMonth(ym);
      var ym1 = addMonth(ym);
      var year0 = ym0[0];
      var month0 = ym0[1];
      var year1 = ym1[0];
      var month1 = ym1[1];

      loadData(year0, month0, function (data) {
        if ($scope.curIndex == 0) {
          $scope.data1 = data;
        } else if ($scope.curIndex == 1) {
          $scope.data0 = data;
        } else if ($scope.curIndex == 2) {
          $scope.data = data;
        }

        loadData(year1, month1, function (data_temp) {
          if ($scope.curIndex == 0) {
            $scope.data = data_temp;
          } else if ($scope.curIndex == 1) {
            $scope.data1 = data_temp;
          } else if ($scope.curIndex == 2) {
            $scope.data0 = data_temp;
          }
          $scope.$apply();
          $ionicSlideBoxDelegate.enableSlide(true);
        });
      });


    }
  });

  $scope.showProce = function (col, index) {
    for (var i = 0; i < col.tasks.length; i++) {
      var task = col.tasks[i];
      if (task.index == index) {
        return parseInt(task.id % 4);
      }
    }
    return -1;
  };

  $scope.showHead = function (col, index) {

    var date = col.date;
    var num = parseInt(date.Format("yyyyMMdd"));
    for (var i = 0; i < col.tasks.length; i++) {
      var task = col.tasks[i];
      if (task.index == index && task.sdt == num) {
        return task.name;
      }
    }
    return '';
  };

  today();

  $scope.dateClick = function (selectedData) {
    $scope.selectedData = selectedData;
  }

  $scope.onSlideChange = function (index) {
    var ym = $scope.currentYM.split('-');
    var ym1 = null;
    var setup = index - $scope.curIndex;
    if (setup == 1 || setup < -1) {
      ym1 = addMonth(ym);
    } else if (setup > 1 || setup == -1) {
      ym1 = minuMonth(ym);
    }
    var nowDate = new Date(ym1[0], ym1[1] - 1, $scope.selectedData.date.getDate());
    var data = index == 0 ? $scope.data0 : index == 1 ? $scope.data : $scope.data1;
    $scope.selectedData = getSelectedData(nowDate, data);
    $scope.curIndex = index;
    $ionicSlideBoxDelegate.enableSlide(false);
    //$timeout(function () {
    //  $ionicSlideBoxDelegate.enableSlide(true);
    //}, 1500);
  }

});
